/*
 * Copyright (c) 2007 jNetX.
 * http://www.jnetx.com
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * jNetX. You shall not disclose such Confidential Information and
 * shall use it only in accordance with the terms of the license
 * agreement you entered into with jNetX.
 *
 * $Id$
 */
package com.jnetx.javax.sip.header;

import javax.sip.address.Address;
import javax.sip.header.ToHeader;
import java.text.ParseException;

/**
 * @author <a href="mailto:dparhonin@jnetx.ru">Dmitry Parhonin</a>
 * @version $Revision$
 */
public class ToHeaderImpl extends ParametrizedHeaderBase implements ToHeader {
    private final static ToHeaderParser parser = new ToHeaderParser();

    private Address address;

    public ToHeaderImpl() {
        super(ToHeader.NAME);
    }

    ToHeaderImpl(byte[] data, int start, int end) {
        super(ToHeader.NAME, data, start, end);
    }

    public Object clone() {
        final ToHeaderImpl header = new ToHeaderImpl();
        header.cloneBase(this);
        if (parsed) {
            header.cloneParameters(this);
            header.address = this.address;
        }
        return header;
    }

    protected HeaderParser getParser() {
        return parser;
    }

    protected String getValueAsString() {
        return address == null ? "<undefined>" : address.toString();
    }

    /**
     * Sets the tag parameter of the ToHeader. The tag in the To field of a
     * request identifies the peer of the dialog. If no dialog is established,
     * no tag is present.
     * <p/>
     * The To Header MUST contain a new "tag" parameter. When acting as a UAC
     * the To "tag" is maintained by the SipProvider from the dialog layer,
     * however whan acting as a UAS the To "tag" is assigned by the application.
     * That is the tag assignment for outbound responses for messages in a
     * dialog is only the responsibility of the application for the first
     * outbound response. If AUTOMATIC_DIALOG_SUPPORT is set to <it>on</it>
     * (default behavior) then, after dialog establishment, the stack will take care
     * of the tag assignment. Null is acceptable as a tag value. Supplying null
     * for the tag results in a header without a tag.
     *
     * @param tag the new tag of the To Header
     * @throws java.text.ParseException which signals that an error has been reached
     *                                  unexpectedly while parsing the Tag value.
     */
    public void setTag(String tag) throws ParseException {
        setParameter("tag", tag);
    }

    /**
     * Gets tag of ToHeader. The Tag parameter identified the Peer of the
     * dialogue.
     *
     * @return the tag parameter of the ToHeader. Returns null if no Tag is
     *         present, i.e no dialogue is established.
     */
    public String getTag() {
        return getParameter("tag");
    }

    /**
     * Sets the Address parameter of this Address.
     *
     * @param address the Address object that represents the new
     *                address of this Address.
     */
    public void setAddress(Address address) {
        parse();
        this.address = address;
        invalidateHeaderData();
    }

    /**
     * Gets the address parameter of this Address.
     *
     * @return the Address of this Address
     */
    public Address getAddress() {
        parse();
        return this.address;
    }
}
/*
 * $Log$
 */